define('p/order-provider/index', ['./mods/pick-date', './mods/upload', './mods/calendar', '../../c/base/index', '../../c/nav/index'], function() {


    $(".fa-circle-o").each(function() {
        if ($(this).parents("a").text() == "拼团交易管理") {
            $(this).parents("a").parents("li").addClass("active");
        }
    });

    //alert(CRM);
    /**
     * 获得商品下拉列表
     */
    function queryLogisticsOrderItem() {
        CRM.request({
            "type": 'POST',
            "url": CRM.getOuterUrl('/trade/order/queryGroupOrderItemForProvider'),
            "dataType": "json",
            contentType: "application/x-www-form-urlencoded",
            "data": {
                orderType: 2
            },
            "success": function(req) {
                if (req.status) {
                    $("#J_ItemId").html("");
                    $("#J_ItemId").append("<option value=''>全部</option>");
                    $.each(req.entry, function(index, entry) {
                        $("#J_ItemId").append("<option value='" + entry.id + "'>" + entry.itemName + "</option>");
                    });
                } else {
                    CRM.showDialog(req.message);
                }
            }
        });
    }
    queryLogisticsOrderItem();
    /**
     * 初始化物流公司
     */
    function queryLogisticsCompanyList() {
        CRM.request({
            "type": 'POST',
            "url": CRM.getOuterUrl('/trade/order/queryLogisticsCompanyList'),
            "dataType": "json",
            contentType: "application/x-www-form-urlencoded",
            "data": {
                //cityName: $("#J_City").val()
            },
            "success": function(req) {
                if (req.status) {
                    $("#U_logisticsCompany").html("");
                    $.each(req.entry, function(index, entry) {
                        $("#U_logisticsCompany").append("<option value='" + entry + "'>" + entry + "</option>")
                    });
                    upload();
                } else {
                    CRM.showDialog(req.message);
                }
            }
        });
    }

    $('#isOutLogistics').on('change', function(e) {
        //queryLogisticsOrderItem();
        //getLogisticsCompanyForCity();
    });
    $('#U_logisticsCompany').on('change', function(e) {
        upload();
    });
    //getLogisticsCompanyForCity();//初始化物流公司
    queryLogisticsCompanyList();
    //warehouseList();
    $('#J_DateEnd').calendar({ format: 'yyyy-MM-dd HH:mm:ss' });
    (function() {
        var tpl = '<div class="dialog-content J_DialogContent" style="top:{top}px;left:{left}px;">' +
            '<em class="dialog-close dialog-iconfont J_DialogClose">&#xe600;</em>' +
            '<p class="dialog-txt">{txt}</p>' +
            '<p class="dialog-btn-content">' +
            '<span class="dialog-btn J_DialogBtn">确定</span>' +
            '</p>' +
            '</div>';
        var loadingTpl = '<div class="dialog-content J_DialogContent J_DialogLoading" style="top:{top}px;left:{left}px;">' +
            // '<em class="dialog-close dialog-iconfont J_DialogClose">&#xe600;</em>'+
            '<p class="dialog-txt"><img src="http://imgsize.52shangou.com/img/n/12/22/1450763881027_2190.gif"></p>' +
            // '<p class="dialog-btn-content">'+
            // '<span class="dialog-btn J_DialogBtn">确定</span>'+
            // '</p>'+
            '</div>';

        var scrollHeight = document.documentElement.scrollHeight;
        var scrlllWidth = document.documentElement.scrollWidth;


        function showMask() {
            $(document.body).append('<div class="mask-dialog J_MaskDialog" style="height:' + scrollHeight + 'px;"></div>');
        }

        function sub(str, data) {
            return str
                .replace(/{(.*?)}/igm, function($, $1) {
                    return data[$1] ? data[$1] : $;
                });
        }

        function bindEvent(el, callback) {
            el.find('.J_DialogClose').on('click', function() {
                el.remove();
                $('.J_MaskDialog').remove();
            });
            el.find('.J_DialogBtn').on('click', function() {
                el.remove();
                $('.J_MaskDialog').remove();
                callback && callback();

            });
        }

        function showDialog(txt, callback) {
            var top = window.scrollY + ($(window).height() / 2 - 81);
            var left = ($(window).width() / 2 - 201);
            var el = $(sub(tpl, {
                left: left,
                top: top,
                txt: txt
            }));
            showMask();
            el.appendTo($(document.body));
            bindEvent(el, callback);
        }

        function showLoading() {
            var top = window.scrollY + ($(window).height() / 2 - 81);
            var left = ($(window).width() / 2 - 201);
            var el = $(sub(loadingTpl, {
                left: left,
                top: top
            }));
            showMask();
            el.appendTo($(document.body));
            // bindEvent(el);
        }


        window.showDialog = showDialog;
        window.showLoading = showLoading;
        window.hideLoading = function() {
            $('.J_MaskDialog,.J_DialogLoading').remove();
        }
    })();


    $('#J_Download').bind('click', function(e) {
        //          var start = $('#J_DateStart').val();
        var end = $('#J_DateEnd').val();
        if (!end) {
            CRM.showDialog('请选择开始日期和结束日期');
            return;
        }
        //if (!$("#J_ItemId").val()) {
        //    CRM.showDialog('没有待发货的商品，无需下载');
        //    return;
        //}
        location.href = CRM.getOuterUrl('/trade/groupOrder/downloadOrderForProvider?' +
            //'startDate='+start +
            'endDate=' + end +
            //'&warehouseCode=' + $('#J_City').val() +
            //'&logisticsCompany=' + $('#J_logisticsCompany').val() +
            //'&isOutLogistics=' + $('#isOutLogistics').val() +
            '&itemId=' + $("#J_ItemId").val());
    });

    $('#J_model_Download').bind('click',function(e){
        location.href=CRM.getOuterUrl('/trade/groupOrder/downloadTemplateForProvider');
    });


    function upload() {
        new Uploader({
            trigger: '#J_UploadLogistics',
            name: 'file',
            action: CRM.getOuterUrl('/trade/groupOrder/uploadDeliveryForProvider'),
            //accept: 'image/*',
            data: {
                // // date:elDataExport.val()
                // startDate: startDate,
                logisticsCompany: $("#U_logisticsCompany").val()
            },
            multiple: false,
            error: function(file) {
                CRM.showDialog('上传失败');
                upload();
                // console.log(file);
                // elDataExport.trigger('blur');
            },
            success: function(data) {
                CRM.showDialog(data.message);
                upload();
                // $('.J_ExportBillDate').trigger('blur');

            }
        });
    }
});
